package LeetCode.leetcode225;

import java.util.LinkedList;
import java.util.Queue;

class MyStack {
    Queue<Integer> queue1 = new LinkedList<>();
    Queue<Integer> queue2 = new LinkedList<>();

    public MyStack() {

    }

    public void push(int x) {

        queue1.offer(x);
    }

    public int pop() {
        int res = 0;
        while (!queue1.isEmpty()) {
            res = queue1.poll();
            if (!queue1.isEmpty()) {
                queue2.offer(res);
            }
        }
        while (!queue2.isEmpty()) {
            queue1.offer(queue2.poll());
        }
        return res;
    }

    public int top() {
        int res = 0;
        while (!queue1.isEmpty()) {
            res = queue1.poll();
            queue2.offer(res);
        }
        while (!queue2.isEmpty()) {
            queue1.offer(queue2.poll());
        }
        return res;
    }

    public boolean empty() {
        return queue1.isEmpty();
    }
}
